/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.model.Classes;
import vn.webapp.model.Courses;
import vn.webapp.model.Coursing;

@Repository("coursesDAO")
@SuppressWarnings({"unchecked", "rawtypes"})
@Transactional
public class CoursesDAOImpl extends BaseDao implements CoursesDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    @Override
    public List<Courses> list() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Courses.class);
            //criteria.setMaxResults(10);
            //criteria.setFirstResult(0);
            List<Courses> courses = criteria.list();
            commit();
            return courses;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public Courses viewDetail(String id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Courses c WHERE c.course_id = '" + id + "'");
        return (Courses) q.uniqueResult();
    }
    
    @Override
    public Courses checkExistsCode(String code, int id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Courses c WHERE c.code = '" + code + "' AND c.course_id != '" + id + "'");
        return (Courses) q.uniqueResult();
    }
    
    @Override
    public int updateTheCourse(String code, String name, int id, int credits){
        Query query = sessionFactory.getCurrentSession().createQuery(
                "UPDATE Courses SET code ='" + code + "', name ='" + name + "', credits = '" + credits + "' WHERE course_id = '" + id + "'");
        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    public int removeCourse(int id) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "DELETE from Courses r WHERE r.course_id ='" + id + "'");
        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    public int saveACourse(Courses course)
    {
        try {
           begin();
           int id = 0; 
           id = (int)getSession().save(course);
           commit();
           return id;           
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<Courses> filter(int page, int pageSize) {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Courses.class);
            criteria.setMaxResults(pageSize);
            if (page > 0) {
                criteria.setFirstResult(page * pageSize);
            } else {
                criteria.setFirstResult(page);
            }
            List<Courses> courses = criteria.list();
            commit();
            return courses;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public int count(){
        //int size = 0;
         try {
            begin();
            Criteria criteria = getSession().createCriteria(Courses.class);            
            List<Courses> courses = criteria.list();
            commit();
            return courses.size();
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    public List<Courses> getById(int courseId){
         try {
        begin();
        Criteria criteria = getSession().createCriteria(Coursing.class, "course");
        List<Courses> courses = new ArrayList<>();
        
        criteria.add(Restrictions.eq("course.course_id", courseId));
        courses = (List<Courses>) criteria.list();
        commit();
        return courses;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }

}
